package com.penghaisoft.microwms.materialhand.web.outwarehouse.service.impl;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.RawOrderOutDto;
import com.penghaisoft.microwms.common.dto.RawOrderOutSnDto;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawOrderOutMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawOrderOutSnMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.service.RawOrderOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王治国 on 2018/5/31.
 */
@Service
public class RawOrderOutServiceImpl extends BaseService implements RawOrderOutService {

    @Autowired
    private RawOrderOutMapper rawOrderOutMapper;

    @Autowired
    private RawOrderOutSnMapper rawOrderOutSnMapper;

    /**
     * 获取领料信息
     *
     * @return
     */
    @Override
    public List<RawOrderOutDto> findOrderInfo(RawOrderOutDto rawOrderOutDto) {

        return rawOrderOutMapper.findOrderInfo(rawOrderOutDto);
    }

    /**
     * 获取领料信息
     *
     * @return
     */
    @Override
    public List<RawOrderOutSnDto> findOrderInfoPin(RawOrderOutDto rawOrderOutDto) {

        int ph = 0;

        List<RawOrderOutDto> dtos = rawOrderOutMapper.findOrderInfo(rawOrderOutDto);
        List<RawOrderOutSnDto> snDtos = rawOrderOutSnMapper.findInfoByPE1(rawOrderOutDto);

        for (RawOrderOutDto dto : dtos){
            ph = 0;
            for (RawOrderOutSnDto snDto : snDtos){

                if (snDto.getModel().equals(dto.getModel())){
                    ph = 1;
                    snDto.setUserDefined2(dto.getUserDefined2());
                    snDto.setRequireAmount(dto.getRequireAmount());
                    snDto.setRealAmount(dto.getRealAmount());
                }
            }
            if (ph == 0){
                RawOrderOutSnDto snDto = new RawOrderOutSnDto();
                snDto.setRequisitionNo(dto.getRequisitionNo());
                snDto.setUserDefined2(dto.getUserDefined2());
                snDto.setContractNo(dto.getContractNo());
                snDto.setModel(dto.getModel());
                snDto.setModelType(dto.getModelType());
                snDto.setRequireAmount(dto.getRequireAmount());
                snDto.setRealAmount(dto.getRealAmount());
                snDto.setAmount(new BigDecimal("0"));
                snDto.setCount(0l);
                snDto.setUnit("");

                snDtos.add(snDto);
            }

        }

        return snDtos;
    }

    /**
     * 获取领料信息
     *
     * @return
     */
    @Override
    public List<RawOrderOutDto> findOrderInfoPin1(RawOrderOutDto rawOrderOutDto) {

/*        int ph = 0;
        List<RawOrderOutDto> objs = new ArrayList<>();

        List<RawOrderOutDto> dtosLW = new ArrayList<>();      // 钢管领料详情
        List<RawOrderOutDto> dtosPE = new ArrayList<>();      // 领料单类型为 2：管件 的PE管领料详情

        List<RawOrderOutSnDto> snDtosLW = new ArrayList<>(); // 钢管明细
        List<RawOrderOutSnDto> snDtosPE = new ArrayList<>(); // 匹配钢管的PE管明细
        List<RawOrderOutSnDto> snDtosPE1 = new ArrayList<>(); // 匹配自身的PE管明细

        List<RawOrderOutDto> dtos = rawOrderOutMapper.findOrderInfo(rawOrderOutDto);

        for (RawOrderOutDto obj : dtos){
            if ("0".equals(obj.getRequisitionType()) && Constant.MODEL_PE.equals(obj.getModelType())){
                snDtosPE = rawOrderOutSnMapper.findInfoByPE2(obj);
            }else if ("2".equals(obj.getRequisitionType()) && Constant.MODEL_PE.equals(obj.getModelType())){
                snDtosPE1 = rawOrderOutSnMapper.findInfoByPE(obj);
                dtosPE.add(obj);
            }else {
                snDtosLW = rawOrderOutSnMapper.findInfoByPE(obj);
                dtosLW.add(obj);
            }
        }

        for (RawOrderOutDto obj : dtosLW){
            if (snDtosLW == null || snDtosLW.isEmpty()){
                obj.setAmount(new BigDecimal("0"));
                obj.setCount(0l);
                objs.add(obj);
            }
            for (RawOrderOutSnDto ob : snDtosLW){
                if (obj.getModelType().equals(ob.getModelType())){
                    obj.setAmount(ob.getAmount());
                    obj.setCount(ob.getCount());
                    objs.add(obj);
                }
            }
        }

        if ("0".equals(dtos.get(0).getRequisitionType())){

            for (RawOrderOutDto obj : objs){
                ph = 0;
                BigDecimal a = obj.getAmount() == null ? BigDecimal.ZERO : obj.getAmount();
                if (snDtosPE == null || snDtosPE.isEmpty()){
                    obj.setAmountPE(new BigDecimal("0"));
                    obj.setCountPE(0l);
                }
                for (RawOrderOutSnDto ojbk : snDtosPE){
                    BigDecimal b = ojbk.getLength() == null ? BigDecimal.ZERO : ojbk.getLength();
                    if ( a.compareTo(b) == 0 ){
                        ph = 1;
                        ojbk.setUserDefined1("1");  // 暂借 udf1 存储PE管匹配状态，设为1的是匹配过的（不为1则为多余的PE管，待退扫）

                        obj.setAmountPE(ojbk.getAmount());
                        obj.setCountPE(ojbk.getCount());
                    }
                }
                // 如果 ph=0 ,说明该规格的钢管尚未匹配PE管
                if (ph == 0){
                    obj.setAmountPE(new BigDecimal("0"));
                    obj.setCountPE(0l);
                }
            }

        }else {
            for (RawOrderOutDto obj : dtosLW){
                if (snDtosPE1 == null || snDtosPE1.isEmpty()){
                    obj.setAmount(new BigDecimal("0"));
                    obj.setCount(0l);
                    objs.add(obj);
                }
                for (RawOrderOutSnDto ob : snDtosPE1){
                    if (obj.getModelType().equals(ob.getModelType())){
                        obj.setAmount(ob.getAmount());
                        obj.setCount(ob.getCount());
                        objs.add(obj);
                    }
                }
            }
            for (RawOrderOutDto ojbk : objs){
                ojbk.setAmountPE(new BigDecimal("0"));
                ojbk.setCountPE(0l);
            }
        }*/
        List<RawOrderOutDto> objs = new ArrayList<>();
        List<RawOrderOutDto> dtos = rawOrderOutMapper.findOrderInfo(rawOrderOutDto);

        String requisitionType = dtos.get(0).getRequisitionType();
        if ("0".equals(requisitionType)){
            objs = findOrderInfoType0(dtos);
        }else if ("2".equals(requisitionType)){
            objs = findOrderInfoType2(dtos);
        }else {
            objs = null;
        }

        return objs;
    }

    /**
     * 1889 wzhg 领料单类型为 0：普通（PE管匹配钢管）的明细
     * @param dtos
     * @return
     */
    public List<RawOrderOutDto> findOrderInfoType0(List<RawOrderOutDto> dtos) {
        int ph = 0;
        List<RawOrderOutDto> objs = new ArrayList<>();   // 明细 list

        List<RawOrderOutDto> dtosLW = new ArrayList<>();      // 钢管领料详情
        List<RawOrderOutSnDto> snDtosLW = new ArrayList<>(); // 钢管明细
        List<RawOrderOutSnDto> snDtosPE = new ArrayList<>(); // 匹配钢管的PE管明细
        // 根据领料任务查询领料明细以及筛选捕捉钢管领料任务
        for (RawOrderOutDto obj : dtos){
            if (Constant.MODEL_PE.equals(obj.getModelType())){
                snDtosPE = rawOrderOutSnMapper.findInfoByPE2(obj);
            }else {
                snDtosLW = rawOrderOutSnMapper.findInfoByPE(obj);
                dtosLW.add(obj);
            }
        }
        // 拼装一：钢管的领料明细进度list拼装(按长度分类)
        for (RawOrderOutDto obj : dtosLW){
            if (snDtosLW == null || snDtosLW.isEmpty()){
                obj.setAmount(new BigDecimal("0"));
                obj.setCount(0l);
                objs.add(obj);
            }
            for (RawOrderOutSnDto ob : snDtosLW){
                if (obj.getModelType().equals(ob.getModelType())){
                    obj.setAmount(ob.getAmount());
                    obj.setCount(ob.getCount());
                    objs.add(obj);
                }
            }
        }
        // 拼装二：把PE管的领料明细进度拼装到对应长度的钢管上
        for (RawOrderOutDto obj : objs){
            ph = 0;
            BigDecimal a = obj.getAmount() == null ? BigDecimal.ZERO : obj.getAmount();
            if (snDtosPE == null || snDtosPE.isEmpty()){
                obj.setAmountPE(new BigDecimal("0"));
                obj.setCountPE(0l);
            }
            for (RawOrderOutSnDto ojbk : snDtosPE){
                BigDecimal b = ojbk.getLength() == null ? BigDecimal.ZERO : ojbk.getLength();
                if ( a.compareTo(b) == 0 ){
                    ph = 1;
                    ojbk.setUserDefined1("1");  // 暂借 udf1 存储PE管匹配状态，设为1的是匹配过的（不为1则为多余的PE管，待退扫）
                    obj.setAmountPE(ojbk.getAmount()); // PE管总长
                    obj.setCountPE(ojbk.getCount());   // PE管根数
                }
            }
            // 如果 ph=0 ,说明该规格的钢管尚未匹配PE管
            if (ph == 0){
                obj.setAmountPE(new BigDecimal("0"));
                obj.setCountPE(0l);
            }
        }

        return objs;
    }

    /**
     * 1889 wzhg 领料单类型为 2；管件（PE管匹配自身长度）的明细
     * @param dtos
     * @return
     */
    public List<RawOrderOutDto> findOrderInfoType2(List<RawOrderOutDto> dtos) {

        int ph = 0;
        List<RawOrderOutDto> objs = new ArrayList<>();

        List<RawOrderOutDto> dtosLW = new ArrayList<>();      // 钢管领料详情
        List<RawOrderOutSnDto> snDtosLW = new ArrayList<>(); // 钢管明细
        List<RawOrderOutSnDto> snDtosPE = new ArrayList<>(); // 匹配钢管的PE管明细

        // 根据领料任务查询领料明细
        for (RawOrderOutDto obj : dtos){
            if (Constant.MODEL_PE.equals(obj.getModelType())){
                snDtosPE = rawOrderOutSnMapper.findInfoByPE(obj);
            }else {
                snDtosLW = rawOrderOutSnMapper.findInfoByPE(obj);
            }
            dtosLW.add(obj);
        }
        // 拼装一：管件和PE管的领料明细进度list拼装(按长度分类)
        for (RawOrderOutDto obj : dtosLW){
            if (snDtosLW == null || snDtosLW.isEmpty()){
                obj.setAmount(new BigDecimal("0"));
                obj.setCount(0l);
                objs.add(obj);
            }
            for (RawOrderOutSnDto ob : snDtosLW){
                if (obj.getModelType().equals(ob.getModelType())){
                    obj.setAmount(ob.getAmount());
                    obj.setCount(ob.getCount());
                    objs.add(obj);
                }
            }
            for (RawOrderOutSnDto ob : snDtosPE){
                if (obj.getModelType().equals(ob.getModelType())){
                    obj.setAmount(ob.getAmount());
                    obj.setCount(ob.getCount());
                    objs.add(obj);
                }
            }
        }
        // 拼装二：后续字段补零（这俩个字段是给领料类型为0的单据使用的，其他类型补零即可）
        for (RawOrderOutDto obj : objs){
            obj.setAmountPE(new BigDecimal("0"));
            obj.setCountPE(0l);

        }
        return objs;
    }

    /**
     * @param rawOrderOutSnDto
     * @return
     * @wzhg 1866 根据【领料单号、合同号、型号】修改【数量】
     */
    @Override
    public Integer updateAmount(RawOrderOutSnDto rawOrderOutSnDto) {
        return rawOrderOutMapper.updateAmount(rawOrderOutSnDto);
    }

    /**
     * @param rawOrderOutDto
     * @return
     * @wzhg 1866 根据【领料单号、合同号、型号】修改【领料单状态：发货中】
     */
    @Override
    public Integer updateOne(RawOrderOutDto rawOrderOutDto) {
        return rawOrderOutMapper.updateOne(rawOrderOutDto);
    }

    /**
     * @param rawOrderOutDto
     * @return
     * @wzhg 1866 根据【领料单号、合同号、型号】修改【领料单状态：扫码完成】
     */
    @Override
    public Integer updateThree(RawOrderOutDto rawOrderOutDto) {
        return rawOrderOutMapper.updateThree(rawOrderOutDto);
    }

    /**
     * @param rawOrderOutDto
     * @return
     * @wzhg 1866 根据【领料单号、合同号、型号】修改【领料单状态：发货完成】
     */
    @Override
    public Integer updateTwo(RawOrderOutDto rawOrderOutDto) {
        return rawOrderOutMapper.updateTwo(rawOrderOutDto);
    }

    /**
     * @wzhg 1868 根据【领料单号、合同号、型号】修改【user_defined1：配套中】
     * @param rawOrderOutDto
     * @return
     */
    @Override
    public Integer updateOfPE(RawOrderOutDto rawOrderOutDto) {
        return rawOrderOutMapper.updateOfPE(rawOrderOutDto);
    }

    /**
     * PE管扫码 修改对应的 钢管信息
     * @wzhg 1868 根据【领料单号、合同号、型号】修改【配套状态】
     * @param rawOrderOutDto
     * @return
     */
    @Override
    public Integer updateInfo(RawOrderOutDto rawOrderOutDto) {
        return rawOrderOutMapper.updateInfo(rawOrderOutDto);
    }

}
